<?php
include('../incsession.php');
if(isset($_POST['dnd_filename'])){
	$_SESSION['dnd_filename']=$_POST['dnd_filename'];
	$_SESSION['do_dnd'] = 1;
	header('location: abstract_redirect.php');	
}


include("timer.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sample page</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen">

</head>

<body onLoad="TimeCountDown(),init()">
<div id="wrapper">
	<div id="header">
		<div id="logo"></div>
		<div id="menu">
        <?php
			include("menu.php");
		?>
       
		</div>
	</div>
	<div class="clearer"></div>
	<div class="edge-top"></div>

	<div id="main">

		<div id="title">大綱練習答案<font size=4></font></div>
<hr/><br><br>

<!-- 內文開始 !-->
<?php
	
	$tid = $_SESSION['topic'];
	$level = $_SESSION['level'];
	
	$sql = "SELECT * FROM `abstract_drag_and_drop` WHERE `topicid` = '".$tid."' AND `level` = '".$level."' ORDER BY `layer`";
	$result = mysql_query($sql, $orcp_con);
	$question_num = mysql_num_rows($result);
	for($i=0;$i<$question_num;$i++)
		$question[$i] = mysql_fetch_row($result);
	
	$sql = "SELECT * FROM `abstract_dnd_answer` WHERE `topicid` = '".$tid."' AND `level` = '".$level."' AND `layer` > 0 ORDER BY `layer`";
	$result = mysql_query($sql, $orcp_con);
	$answer_num = mysql_num_rows($result);
	for($i=0;$i<$answer_num;$i++)
		$right_answer[$i] = mysql_fetch_row($result);
	
	$sql = "SELECT * FROM `abstract_dnd_answer` WHERE `topicid` = '".$tid."' AND `level` = '".$level."'";
	$result = mysql_query($sql, $orcp_con);
	$answer_num = mysql_num_rows($result);
	for($i=0;$i<$answer_num;$i++)
		$all_answer[$i] = mysql_fetch_row($result);
	
	for($i=0;$i<$question_num;$i++)
		$user_answer[$i] = "";
	$dnd_result = $_POST['dnd_result'];
	//echo $dnd_result."<br>";
	
	
	$arr = explode("&", $dnd_result);

	if($dnd_result != "")
		foreach ($arr as $p)
		{
			list($id, $tbl, $row, $col) = explode('_', $p);
			//echo "row ".$row."<br>";
			//echo "chunk ".$chunk[$j]."<br>";
			//echo $id."&nbsp;&nbsp;&nbsp;&nbsp;".($row-$j-1)."&nbsp;&nbsp;&nbsp;&nbsp;".$p."<br>";
			for($i=0;$i<$answer_num;$i++)
				if($id == $all_answer[$i][0])
					$user_answer[$row-1] = $all_answer[$i][1];
			//print "Id=$id Tbl=$tbl Col=$col Row=$row<br>";
		}
	
	$point = 0;
	for($i=0;$i<$question_num;$i++)
	{
		if( $right_answer[$i][1] == $user_answer[$i] )
			$point++;
	}
	
	if($question_num != 0)
		$point = ($point*1.0)*100/$question_num;
	echo "你的分數：".$point."%<br><br><br>";

	mysql_select_db($db_name, $orcp_con);
	$query="INSERT INTO score (uid, score, topicid, level, classid) value (".$_SESSION['uid'].", ".$point.", ".$_SESSION['topic'].", ".$_SESSION['level'].", ".$_SESSION['class'].")";
	//echo $query;
	$result = mysql_query($query);
	
	// Export xls
	/** Error reporting */ 
	//error_reporting(E_ALL); 
	
	/** PHPExcel */ 
	//require_once "Classes\PHPExcel.php"; 
	
	/** PHPExcel_IOFactory */ 
	//require_once "Classes\PHPExcel\IOFactory.php";
	
	$sql = "SELECT name FROM `topic` WHERE `topicid` = '".$tid."'";
	$result = mysql_query($sql, $orcp_con);
	$tname = mysql_fetch_row($result);
	$filename = "./dnd_answer/".$tname[0]."_".$_SESSION['topic']."_".$level.".xls";
	
	//$objPHPExcel = new PHPExcel(); 
	//$objPHPExcel->setActiveSheetIndex(0);

	echo "<table>";
	echo "<colgroup><col width='80'/><col width='440'/><col width='440'/></colgroup>\n";
	echo "<tr  style='background-color: #FFF'><td>段落</td><td>你的答案</td><td>正確答案</td></tr>";
	//$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setSize(16);
	//$objPHPExcel->getActiveSheet()->setCellValue("A1","題目敘述");
	//$objPHPExcel->getActiveSheet()->getStyle("B1")->getFont()->setSize(16);
	//$objPHPExcel->getActiveSheet()->setCellValue("B1","正確答案");
	//$num1=0;
	for($i=0;$i<$question_num;$i++)
	{
		$question[$i][1] = str_replace("&#39;", "'", $question[$i][1]);
		$question[$i][1] = str_replace('&quot;', '"', $question[$i][1]);
			if($question[$i][2]==10000){
			$num1++;
			echo "<tr height=20px style='background-color: white'><td><td>";
			//echo "<tr><td  style='background-color: #ADDFFF' ><font size=4pt>".$question[$i][1]."</font></td>\n";
			echo "<tr><td  style='background-color: #ADDFFF' ><font size=4pt>".$num1."</font></td>\n";
			//$num2[$num1]=0;
		/*	$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->getFont()->setSize(16);
			$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->applyFromArray( 
			array("fill"     => array( 
										"type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "ADDFFF") 
									), ) );
			
			$objPHPExcel->getActiveSheet()->setCellValue("A".($i+2),$question[$i][1]);*/
		}
		if((int)($question[$i][2]/10000)!=1 && (int)($question[$i][2]/10000)!=(int)($question[$i-1][2]/10000)){
			$num1++;
			echo "<tr height=20px style='background-color: white'><td><td>";
			//echo "<tr><td  style='background-color: #ADDFFF'><font size=4pt>".$question[$i][1]."</font></td>\n";
			echo "<tr><td  style='background-color: #ADDFFF'><font size=4pt>".$num1."</font></td>\n";
			//$num2[$num1]=0;
		/*	$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->getFont()->setSize(16);
			$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->applyFromArray( 
			array("fill"     => array( 
										"type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "ADDFFF") 
									), ) );
			$objPHPExcel->getActiveSheet()->setCellValue("A".($i+2),$question[$i][1]);*/
		}
		else if($question[$i][2]!=10000 && (int)($question[$i][2]/10000)==(int)($question[$i-1][2]/10000)){		
			if((int)($question[$i][2]/100)!=1 && (int)($question[$i][2]/100)!=(int)($question[$i-1][2]/100)){
				$num2[$num1]++;
				//echo "<tr><td style='background-color: #BDEDFF'><font size=4pt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$question[$i][1]."</font></td>\n";
				echo "<tr><td style='background-color: #BDEDFF'><font size=4pt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$num1."-".$num2[$num1]."</font></td>\n";
				/*$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->getFont()->setSize(16);
				$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->applyFromArray( 
				array("fill"     => array( 
										"type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "BDEDFF") 
									), ) );
				$objPHPExcel->getActiveSheet()->setCellValue("A".($i+2),"    ".$question[$i][1]);*/
				$num3[$num1][$num2[$num1]]=0;
			}else{
				$num3[$num1][$num2[$num1]]++;
				//echo "<tr><td style='background-color: #E0FFFF'><font size=3pt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$question[$i][1]."</font></td>\n";
				echo "<tr><td style='background-color: #E0FFFF'><font size=3pt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$num1."-".$num2[$num1]."-".$num3[$num1][$num2[$num1]]."</font></td>\n";
				/*$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->getFont()->setSize(14);
				$objPHPExcel->getActiveSheet()->getStyle("A".($i+2))->applyFromArray( 
				array("fill"     => array( 
										"type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "E0FFFF") 
									), ) );
				$objPHPExcel->getActiveSheet()->setCellValue("A".($i+2),"          ".$question[$i][1]);*/
			}
		}
		 
		echo "<td width=325px style='background-color: #e2e2e2'><font size=4>".$user_answer[$i]."</font></td>\n";
		echo "<td width=325px style='background-color: #e2e2e2'><font size=4>".$right_answer[$i][1]."</font></td>\n";	
	/*	$objPHPExcel->getActiveSheet()->getStyle("B".($i+2))->getFont()->setSize(14);
		$objPHPExcel->getActiveSheet()->getStyle("B".($i+2))->applyFromArray( 
				array("fill"     => array( 
										"type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "e2e2e2") 
									), ) );
		$right_answer[$i][1] = str_replace("&#39;", "'", $right_answer[$i][1]);
		$right_answer[$i][1] = str_replace('&quot;', '"', $right_answer[$i][1]);
		$objPHPExcel->getActiveSheet()->setCellValue("B".($i+2),$right_answer[$i][1]);*/
		echo "</tr>\n";
	}
	echo "</table>\n";
	
	/*$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5"); 
	$objWriter->save($filename);*/
?>
<form method = POST action="abstract_dnd_answers.php" >
<br><br>
<div class="btn_right"><input type="submit" name = "next" value = "下一步"></div>
<input type="hidden" name="dnd_filename" value="<?php echo $filename?>">
</form>
<!-- 內文結束 !-->

	</div>
    <div class="edge-bottom"></div>
    <div id="footer"><?php  include("footer.php"); ?></div>
</div>
	
<div id="txt">18:27</div>



</body>
</html>
